package com.bigfreakindeals.DAO;

import java.util.LinkedList;
import java.util.List;

import com.bigfreakindeals.Models.UserProfile;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PROFILEDAO extends DAO {
	protected static final String createSQL = "CREATE TABLE IF NOT EXISTS contactgroup (username STRING PRIMARY KEY, slugID TEXT)";
	
	public PROFILEDAO(Context context, DatabaseHelper dbHelper) {
		super(context, "profile", dbHelper);
	}

	@Override
	protected void createTable() {
		SQLiteDatabase db = null;
		try { 
			db = this.getDatabase();
			db.execSQL(createSQL);
		} catch( Exception ex ) {
		}
	}
	
	public void deleteGroup(int groupID){
		SQLiteDatabase db = null;
		try {
			db = this.getDatabase();
			db.delete(this.tableName, "username = ?", new String [] {String.valueOf(groupID)} );
		} catch( Exception ex ) {
		}	
	}
	public boolean insertGroup(UserProfile profile) {
		ContentValues values = new ContentValues();
		values.put("username", profile.getUsername());
		values.put("slugID", profile.getPrefferedUniversitySlug());
		values.put("password", profile.getPassword());
		
		return this.insertTable(values, "username = ?", new String [] {String.valueOf(profile.getUsername())});
	}	
	
	

	public UserProfile getprofile() {
		String query = "SELECT * FROM profile order by upper(username) asc";
		SQLiteDatabase db = null;
		Cursor cursor = null;
		UserProfile profile =null;
		try {
			db = this.getDatabase();
			cursor = db.rawQuery(query, null);
			if( cursor != null && cursor.getCount() > 0 ) {
				cursor.moveToFirst();
	
				while(cursor.isAfterLast()==false) {
					String username  = cursor.getString(cursor.getColumnIndex("username"));
					String password = cursor.getString(cursor.getColumnIndex("slugID"));
					String univSlug = cursor.getString(cursor.getColumnIndex("password"));
					
					profile = new UserProfile();
					profile.setUsername(username);
					profile.setPassword(password);
					profile.setPrefferedUniversitySlug(univSlug);
					break;	
				}
			}
		} catch( Exception ex ) {
		}finally{
			if ( cursor!=null ){
				cursor.close();	
				cursor = null;
			}
		}
		return profile;
	}
}
